<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <!-- 表单 -->
      <el-form :model="form" :rules="rules" status-icon ref="form">
        <el-form-item prop="mobile">
          <el-input placeholder="请输入手机号码" v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input placeholder="请输入验证码" v-model="form.code" @keyup.enter.native="login"></el-input>
        </el-form-item>
        <el-form-item prop="isAgree">
          <el-checkbox v-model="form.isAgree">我已阅读用户协议和隐私条款</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  // 给组件一个名字，方便调试
  name: 'Login',
  data () {
    return {
      // 表单数据
      form: {
        mobile: '13911111111',
        code: '246810',
        isAgree: true
      },
      // 校验规则
      rules: {
        mobile: [
          // required: 必填的规则
          // message: 错误提示消息
          // trigger: 校验的时机
          { required: true, message: '手机号码不能为空', trigger: ['change', 'blur'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号码格式错误', trigger: ['change', 'blur'] }
          // { min: 5, max: 11, message: '手机号必须是5-11位', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: ['change', 'blur'] },
          { pattern: /^\d{6}$/, message: '验证码格式错误', trigger: ['change', 'blur'] }
        ],
        /*
          自定义校验规则
          { validator: 校验函数, trigger: '' }
        */
        isAgree: [
          {
            validator: (rule, value, callback) => {
              // if (value) {
              //   // 如果值为true，直接通过
              //   callback()
              // } else {
              //   callback(new Error('请阅读隐私协议'))
              // }
              value ? callback() : callback(new Error('请阅读隐私协议'))
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    login () {
      // 先对整个表单进行校验
      this.$refs.form.validate(valid => {
        if (valid) {
          // 表单校验通过，应该发送ajax请求
          login(this.form.mobile, this.form.code).then(res => {
            // this.$message({
            //   type: 'success',
            //   message: '登陆成功'
            // })
            // console.log(res)
            this.$message.success('登陆成功')
            // localStorage.setItem('hmpc-74-token', res.data.data.token)
            setToken(res.data.token)
            // 跳转到首页
            this.$router.push('/')
          }).catch(() => {
            // this.$message({
            //   type: 'error',
            //   message: '登陆失败'
            // })
            this.$message.error('登陆失败')
          })
        } else {
          console.log('表单校验失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
// 铺满整屏
.login-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("~@/assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    width: 400px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("~@/assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
